﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyBasicLib.Data;
using MyBasicLib.Utilities;
using Moq;
using MyBasicLib.Data.ORMLiteNew;
using RUC.Core.Entities;

namespace MyBasicLib.Test.Data.ORMLiteV2
{
    [TestClass]
    public class DataContextTest
    {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            var executor = IOC.DefaultContainer.Resolve<IDatabase>();
            var mock = Moq.Mock.Get(executor);
            mock.Setup(p => p.CreateCommand(It.IsAny<string>())).Returns<string>(p => new System.Data.SqlClient.SqlCommand((string)p)); ;
            mock.Setup(p => p.Execute<int>(null, It.IsAny<System.Data.SqlClient.SqlCommand>())).Returns(100);
            mock.Setup(p => p.Execute<int>(null, It.IsAny<System.Data.SqlClient.SqlCommand>())).Returns(100);


        }
        [TestMethod]
        public void TestMethod1()
        {
            var dc = new DataContext();
            dc.Insert(new App());
            Assert.IsNotNull(System.Transactions.Transaction.Current);
            dc.SubmitChanges();
            Assert.IsNull(System.Transactions.Transaction.Current);
        }
        [TestMethod]
        public void TestMethod2()
        {
            var dc = new DataContext();
            dc.Insert(new App());
            Assert.IsNotNull(System.Transactions.Transaction.Current);
            dc.DiscardChanges();
            Assert.IsNull(System.Transactions.Transaction.Current);
        }
        [TestMethod]
        public void TestMethod3()
        {
            var dc = new DataContext();
            dc.Insert(new App());
            System.Threading.Thread.Sleep(TimeSpan.FromMinutes(.5));
            Assert.IsNull(System.Transactions.Transaction.Current);
        }
    }
}
